package cn.com.yxw.webapp.controller;

import cn.com.yxw.webapp.pojo.Admin;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/5/23.
 */
public class UserListExcelView extends AbstractExcelView {


    protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception{
        response.setHeader("Content-Disposition", "inline;filename=" + new String("用户列表.xls".getBytes(),"iso8859-1"));
        List<Admin> list = (List<Admin>)model.get("list");

        HSSFSheet sheet = workbook.createSheet("users");
        HSSFRow header = sheet.createRow(0);
        header.createCell(0).setCellValue("账号");
        header.createCell(1).setCellValue("姓名");
        header.createCell(2).setCellValue("生日");
        int rowNum = 1;
        for (Admin user : list) {
            HSSFRow row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(user.getUsername());
            row.createCell(1).setCellValue(user.getPassword());
            row.createCell(2).setCellValue(user.getKey());
        }
    }


}
